package com.virtualtrainer_01.dao;

import com.virtualtrainer_01.model.Food;
import com.virtualtrainer_01.model.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;

public class FoodAlimentDB extends SQLiteDB {
	public static final String KEY_ID = "_id";
	public static final String KEY_IDFOOD = "idFood";
	public static final String KEY_IDALIMENT = "idAliment";

	private static final String DATABASE_TABLE = "foodaliment";

	public FoodAlimentDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	@Override
	public String getData() {
		String[] columns = new String[] { KEY_ID, KEY_IDALIMENT, KEY_IDFOOD };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iIdAl = c.getColumnIndex(KEY_IDALIMENT);
		int iIdFo = c.getColumnIndex(KEY_IDFOOD);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iIdAl)
					+ " " + c.getString(iIdFo);
		return result;
	}

	/**
	 * Insert the aliment list in a Food object
	 * 
	 * @param Food
	 */
	@Override
	public long createEntry(Model entry) {
		Food f = (Food) entry;
		FoodDB fdb = new FoodDB(this.ourContext);
		AlimentDB adb = new AlimentDB(this.ourContext);
		fdb.open();
		adb.open();
		ContentValues cv = new ContentValues();
		for (int i = 0; i < f.getAlimentList().size(); i++) {
			try {
				cv.put(KEY_IDALIMENT, adb.getId(f.getAlimentList().get(i)));
				cv.put(KEY_IDFOOD, fdb.getId(entry));
				ourDatabase.insert(DATABASE_TABLE, null,cv);
			} catch (SQLiteException e) {
				e.printStackTrace();
			}
		}
		fdb.close();
		adb.close();
		return 1;
	}
}
